<template>
    <div class="announcement">
      <el-card
        v-for="item in announcements"
        :key="item.id"
        class="announcement-card"
        :class="getCategoryClass(item.category)"
      >
        <div class="card-header">
          <span class="category">{{ item.categoryText }}</span>
        </div>
        <h3>{{ item.title }}</h3>
        <p>{{ item.content }}</p>
        <div class="card-footer">
          <span class="date">{{ formatDate(item.date) }}</span>
          <span class="author">发布者：{{ item.author }}</span>
        </div>
      </el-card>
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue';
  import { listNotices } from '@/api/notice';
  
  const announcements = ref([]);
  
  // 获取数据
  const getData = () => {
    listNotices().then(res => {
      if (res.code === 200) {
        announcements.value = res.data.map(item => ({
          ...item,
          categoryText: getCategoryText(item.category), // 添加类别文本
        }));
      }
    });
  };
  
  // 获取公告类别文本
  const getCategoryText = (category) => {
    switch (category) {
      case 0: return '行政通知';
      case 1: return '活动公告';
      case 2: return '政策信息';
      case 3: return '其他';
      default: return '未知';
    }
  };
  
  // 获取类别样式
  const getCategoryClass = (category) => {
    switch (category) {
      case 0: return 'category-admin';
      case 1: return 'category-event';
      case 2: return 'category-policy';
      case 3: return 'category-other';
      default: return '';
    }
  };
  
  // 日期格式化
  const formatDate = (dateStr) => {
    const date = new Date(dateStr);
    return date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit',
      hour12: false,
    }).replace(/\//g, '-');
  };
  
  // 页面加载完成后获取数据
  setTimeout(() => {
    getData();
  }, 0);
  </script>
  
  <style scoped>
  .announcement {
    margin-top: 20px;
  }
  
  .announcement-card {
    margin-bottom: 10px;
    position: relative;
    padding: 20px;
    backdrop-filter: blur(10px); /* 添加模糊效果 */
    border-radius: 8px;
    color: #fff; /* 字体颜色设置为白色 */
  }
  
  .card-header {
    position: absolute;
    top: 10px;
    right: 10px;
  }
  
  .category {
    padding: 5px 10px;
    border-radius: 3px;
    color: #fff;
  }
  
  .category-admin {
    background: linear-gradient(135deg, rgba(76, 175, 80, 0.7), rgba(76, 175, 80, 0.9)); /* 绿色渐变 */
  }
  
  .category-event {
    background: linear-gradient(135deg, rgba(33, 150, 243, 0.7), rgba(33, 150, 243, 0.9)); /* 蓝色渐变 */
  }
  
  .category-policy {
    background: linear-gradient(135deg, rgba(255, 152, 0, 0.7), rgba(255, 152, 0, 0.9)); /* 橙色渐变 */
  }
  
  .category-other {
    background: linear-gradient(135deg, rgba(156, 39, 176, 0.7), rgba(156, 39, 176, 0.9)); /* 紫色渐变 */
  }
  
  .card-footer {
    display: flex;
    justify-content: space-between;
    margin-top: 20px;
  }
  
  .date {
    color: rgba(255, 255, 255, 0.8); /* 半透明白色 */
  }
  
  .author {
    color: rgba(255, 255, 255, 0.8); /* 半透明白色 */
  }
  </style>